﻿using System;
using System.Windows;
using System.Xml.Serialization;
using nRoute.Components;
using nRoute.Controllers;
using nRoute.Utilities;
using nRoute.Navigation;
#if (WINRT)
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml;
#else
using System.Windows.Markup;
#endif

namespace nRoute.SiteMaps
{
#if (!SILVERLIGHT && !WINRT)
    [Serializable]
#endif
    [XmlRoot(Namespace = SiteMaps.XMLNAMESPACE)]
#if (WINRT)
    [ContentProperty(Name = "ChildNodes")]
#else
    [ContentProperty("ChildNodes")]
#endif
    public class ControllerActionNode
		 : SiteMapNode
    {
        public static readonly DependencyProperty UrlProperty = DependencyProperty.Register("Url",
            typeof(string), typeof(ControllerActionNode), new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE));

        public static readonly DependencyProperty UrlParametersProperty = DependencyProperty.Register("UrlParameters",
            typeof(DependencyParameterCollection), typeof(ControllerActionNode), new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE));

        public static readonly DependencyProperty HandlerNameProperty = DependencyProperty.Register("HandlerName",
            typeof(string), typeof(ControllerActionNode), new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE));

#region Properties

        [XmlElement(Namespace = SiteMaps.XMLNAMESPACE, IsNullable = false)]
        public string Url
        {
            get { return Convert.ToString(GetValue(UrlProperty)); }
            set { SetValue(UrlProperty, value); }
        }

        [XmlArray(Namespace = SiteMaps.XMLNAMESPACE)]
        public DependencyParameterCollection UrlParameters
        {
            get { return (DependencyParameterCollection)GetValue(UrlParametersProperty); }
            set { SetValue(UrlParametersProperty, value); }
        }

        [XmlElement(Namespace = SiteMaps.XMLNAMESPACE)]
        public string HandlerName
        {
            get { return Convert.ToString(GetValue(HandlerNameProperty)); }
            set { SetValue(HandlerNameProperty, value); }
        }

#endregion

#region Overrides

        public override void Execute()
        {
            Guard.ArgumentNotNullOrWhiteSpace(Url, "Url");
            if (!this.IsEnabled) return;

            ControllerService.Execute(
                new ControllerActionRequest(this.Url, this.UrlParameters, this.SiteArea, string.IsNullOrEmpty(HandlerName) ? 
                NavigationService.DefaultNavigationHandler : NavigationService.GetNavigationHandler(HandlerName)));
        }
        
//#if (WP7)

//        protected override void OnAttached()
//        {
//            base.OnAttached();
//            AttachUrlParameters();
//        }

//        protected override void OnDetaching()
//        {
//            base.OnDetaching();
//            DetachUrlParameters();
//        }

//#endif

#endregion

#region Helpers

//#if (WP7)

//        private void AttachUrlParameters()
//        {
//            if (this.UrlParameters != null)
//            {
//                ((IAttachedObject)this.UrlParameters).Attach(this.AttachTarget);
//            }
//        }

//        private void DetachUrlParameters()
//        {
//            if (this.UrlParameters != null)
//            {
//                ((IAttachedObject)this.UrlParameters).Detach();
//            }
//        }

//#endif

#endregion

    }
}
